<% layout('/layouts/default.html', {title: '指标汇总', libs: ['layout','dataGrid','inputmask','ueditor']}){ %>
<div class="ui-layout-west">
	<div class="main-content">
		<div class="box box-main">
			<div class="box-header" style="padding-bottom: 4px;">
				<div class="box-title">
					<i class="fa ">	<a href="#"  class="btn btn-primary" onclick="saveVersion()"  id="save"  style="" title="${text("保存版本")}">保存版本</a>
						</i>
					<i class="fa "><a href="#" onclick="publishVersion()" class="btn btn-primary" id="push" style="" title="${text("发布")}"> 发布</a></i>
				</div>
			</div>
			<#form:hidden path="versionNumber" id="mainVersion"/>
			<#form:hidden path="remarks4" id="dataInfo"/>
			<#form:hidden path="remarks2" id="version_name"/>
			<#form:hidden path="remarks3" id="batch_flag"/>
			<#form:hidden path="name_title" id="name_title"/>
			<div class="box-body">
				<div class="input-group">
					<#form:input path="taskDesc" maxlength="1000" class="form-control " placeholder="请输入单位名称" autocomplete="off"/>
					<span class="input-group-addon" onclick="searchTaskDesc()" style="cursor: pointer">查询</span>
					<span class="input-group-addon" onclick="reset()" style="cursor: pointer">重置</span>
				</div>
			</div>
		</div>
		<div class="demo-class" >
			<table id="leftDataGrid"></table>
		</div>
	</div>
</div>
<div class="ui-layout-center">
	<iframe id="mainFrame" name="mainFrame" class="ui-layout-content p0"></iframe>
</div>
<% } %>
<style>
	#leftDataGrid tr{
		cursor: pointer;
	}
	.SelectBG td{
		background: #eaeaea !important;
		background-color: #eaeaea !important;
		color:blue;
	}


</style>
<script>
	// 初始化布局
	$('body').layout({
		west__initClosed: $(window).width() <= 767, // 是否默认关闭
		west__size: 200,
		onresize_end: function(){
			$('#leftDataGrid').dataGrid('resize');
		}
	});
	var win = $("#mainFrame")[0].contentWindow;
	let dcSupExamSumQuota =${toJson(dcSupExamSumQuota)};
	let isPush =${toJson(isPush)};

	// 展示左侧被考核单位数据
	$('#leftDataGrid').dataGrid({
		data: ${toJson(assessmentUnit)},
		datatype: "local", // 设置本地数据
		sortableColumn:false, // 禁止排序
		autoGridHeight: function(){
			var height = $(window).height()-170;
			return height;
		}, // 设置自动高度
		columnModel: [
			{header:'${text("任务id")}', name:'examId',hidden:true, width:150, align:"left"},
			{header:'${text("考核单位code")}', name:'exameOrg',hidden:true, width:150, align:"left"},
			{header:'${text("被考核单位")}', name:'exameOrgName', width:150, align:"left",classes:""
			},
		],
		emptyDataHint: true,
		altRows: false,
		// 选择行后调用（id:行id，isSelect：是否是选中，event：事件）
		onSelectRow: function(id, isSelect, event) {
			let selectRow = $('#leftDataGrid').dataGrid('getRowData', id);
			$('#mainFrame').attr('src', '${ctx}/quota/dcSupExamSumQuotaInfo?examId='+dcSupExamSumQuota.examId+'&exameOrg='+selectRow.exameOrg+'&exameOrgName='+selectRow.exameOrgName+'&examYear='+dcSupExamSumQuota.remarks2+'&versionNumber='+$("#mainVersion").val()+'&remarks3='+$("#batch_flag").val());
		},
		// 加载成功默认设置选择第一个
		ajaxSuccess: function(data){
			if (data.total>0){
				let row=data.rows[0];
				reloadIndex();

				$('#mainFrame').attr('src', '${ctx}/quota/dcSupExamSumQuotaInfo?examId='+dcSupExamSumQuota.examId+'&exameOrg='+row.exameOrg+'&exameOrgName='+row.exameOrgName+'&examYear='+dcSupExamSumQuota.remarks2+'&versionNumber='+$("#mainVersion").val()+'&remarks3='+$("#batch_flag").val());
			}
		}
	});
	
	// 提供方法，给子页面调用，用来刷新左侧的被考核单位
	function refreshAssessmentUnit() {
		$.ajax({
			type: 'get',
			url: "${ctx}/quota/dcSupExamSumQuota/getLatestAssessmentUnit?examId="+dcSupExamSumQuota.examId,
			success:function(res){
				$('#leftDataGrid').dataGrid('setParam', {
					data: res.data,
					page: 1, rowNum: 5000
				}, true).dataGrid('reloadGrid');
				// 选择第一个展示其指标信息
				if (res.data.length > 0){
					let row = res.data[0];
					reloadIndex();
					$('#mainFrame').attr('src', '${ctx}/quota/dcSupExamSumQuotaInfo?examId='+dcSupExamSumQuota.examId+'&exameOrg='+row.exameOrg+'&exameOrgName='+row.exameOrgName+'&examYear='+dcSupExamSumQuota.remarks2+'&versionNumber='+$("#mainVersion").val()+'&remarks3='+$("#batch_flag").val());
				}
			}
		});
	}

	function refreshStyle(){
		$('#leftDataGrid tr').siblings().removeClass('ui-state-highlight SelectBG')
		$('#leftDataGrid tr').eq(1).addClass('ui-state-highlight');
		$('#leftDataGrid tr').eq(1).addClass('SelectBG');
	}

	function reloadIndex(){
		$('#leftDataGrid tr').eq(1).addClass('SelectBG');
		$('#leftDataGrid tr').click(function(){
			$(this).siblings().removeClass('SelectBG');
			$(this).addClass('SelectBG');
		})
	}

	$('.demo-class').attr('class','position: relative; visibility: visible;');
	// 任务信息的onchange事件
	function searchTaskDesc() {
		// 获取用户输入查询项
		let tempTaskDesc = $("#taskDesc").val();
		if(tempTaskDesc != '' && tempTaskDesc != undefined){
			// 记录目标值
			let targetList = [];
			// 遍历表格数据，获取目标值
			let tempList = ${toJson(assessmentUnit)}
			for(let i = 0; i < tempList.length; i++){
				if(tempList[i].exameOrgName.indexOf(tempTaskDesc) != -1){
					targetList.push(tempList[i]);
				}
			}
			// 刷新表格数据
			$('#leftDataGrid').dataGrid('setParam', {
				data:targetList
			}, true).dataGrid('reloadGrid');
		}else {
			$('#leftDataGrid').dataGrid('setParam', {
				data:${toJson(assessmentUnit)}
			}, true).dataGrid('reloadGrid');
		}
	}

	// 重置表格数据
	function reset() {
		// 重置查询项
		$("#taskDesc").val('');
		// 重置表格数据
		$('#leftDataGrid').dataGrid('setParam', {
			data:${toJson(assessmentUnit)}
		}, true).dataGrid('reloadGrid');
	}

	function saveVersion(){
		$("#batch_flag").val("0");
	// 	js.layer.open({
	// 		type: 2,
	// 		title: ['确认版本'],
	// 		area: ['800px', '500px'],
	// 		scrollbar: true,
	// 		shadeClose: false,
	// 		content: "${ctx}/quota/dcSupExamSumQuotaInfo/saveVersionPage?versionNumber="+$("#mainVersion").val()+"&examId=${dcSupExamSumQuota.examId}&remarks2="+$("#version_name").val(),
	// 			btn1: function (index, layero) {
	// 	},
	// 	end:function () {
	// 			// 保存版本后 字段置为空  用来区分 非批量修改时和批量修改时保存版本处理
	// 		$("#version_name").val(null);
	// 		$('#leftDataGrid').dataGrid('setParam', {
	// 			data:${toJson(assessmentUnit)}
	// 		}, true).dataGrid('reloadGrid');
	// 	}
	// });
		layer.open({
			type: 2,
			title: ['确认版本'],
			area: ['800px', '600px'],
			// groupId 随便传  isGroupOptions 非常用组功能使用传 0  type  1 为区县 2 为市直
			content: "${ctx}/quota/dcSupExamSumQuotaInfo/saveVersionPage?versionNumber="+$("#mainVersion").val()+"&examId=${dcSupExamSumQuota.examId}&remarks2="+$("#version_name").val(),
			success: function(layero, index){
			},end:function (){
						$("#version_name").val(null);
						$('#leftDataGrid').dataGrid('setParam', {
							data:${toJson(assessmentUnit)}
						}, true).dataGrid('reloadGrid');
			}
		})

	}

	function publishVersion() {
		$("#batch_flag").val("0");
		if ($("#mainVersion").val() == ''){
			js.showMessage("初始版本不允许发布，请先保存版本后操作!!!");
			return;
		}
		// var data=JSON.parse($("#dataInfo").val());
		// for (let o in data){
		// 	if (data[o].quotaType ==''
		// 			|| data[o].quotaWeight == ''
		// 			|| data[o].quotaGroup == ''
		// 			|| data[o].sortNum == ''
		// 			|| data[o].quotaContent == ''
		// 			|| data[o].currentYaerGoal == ''
		// 			|| data[o].gradeOrg == ''
		// 			|| data[o].gradeOrg == '|'
		// 	){
		// 		js.showMessage("请检查指标类别、指标类型、序号、权数、本年度目标以及考核评分单位字段是否填写！")
		// 		return;
		// 	}
		// }
		js.confirm('确认发布吗？', "${ctx}/quota/dcSupExamSumQuotaInfo/publish?versionNumber="+$("#mainVersion").val()+"&examId=${dcSupExamSumQuota.examId}",
				function(data){
					js.showMessage(JSON.parse(JSON.stringify(data)).message)
					var result = JSON.parse(JSON.stringify(data)).result;
					if(result == Global.TRUE){
						$('#leftDataGrid').dataGrid('refresh', 1, 20);
						$('#leftDataGrid tr').eq(1).addClass('SelectBG')
					}

				}, 'json', true, '正在发布...');
	}
</script>
